export default {
  computed: {
    curPens(){
      return this.$parent.$parent.canvas.data.pens;
    },
  },
  methods: {
    calcProcessbomAllMap(){
      const that = this;
      const processbomAllMap = {};
      // 按物料编码划分
      that.curPens.forEach( element =>{
        if ( element.data && element.data.key === 'process'){
          element.data.configTree.forEach((elem, index) => {
            elem.children.forEach((el, inD) => {
              if ( el.bomData && el.bomData.length > 0 ){
                el.bomData.forEach( e =>{
                  if (processbomAllMap[e.mcode]){
                    processbomAllMap[e.mcode].push(e);
                  }else {
                    processbomAllMap[e.mcode] = [];
                    processbomAllMap[e.mcode].push(e);
                  }
                });
              }
            })
          })
        }
      })
      return processbomAllMap;
    },
    // 计算当前剩余当前工站物料可用数量 并更新到canvas中
    calcuAndUpdateRemainQuantity(){
      const that = this;
      let processbomAllMap = this.calcProcessbomAllMap();
      that.curPens.forEach( element =>{
        if ( element.data && element.data.key === 'process'){
          element.data.configTree.forEach((elem, index) => {
            elem.children.forEach((el, inD) => {
              if ( el.bomData && el.bomData.length > 0 ){
                el.bomData.forEach( elb =>{
                  if (processbomAllMap[elb.mcode]){
                    // 物料可用数量 = 总数量 - 所有已分配给工站的数量 - 已经分配给自己的
                    let allStationsRequired = processbomAllMap[elb.mcode].map( el => el.stationsRequired );
                    if ( allStationsRequired.length > 0 ){
                      allStationsRequired = allStationsRequired.reduce((total, num) => (total + num));
                    }else {
                      allStationsRequired = 0;
                    }
                    elb.remainQuantity = elb.allQuantity - allStationsRequired;
                    elb.remainQuantityOrigin = elb.remainQuantity;
                    elb.allStationsRequired = allStationsRequired; // 所有工站已用的数量
                    elb.stationsRequiredOrigin = elb.stationsRequired;
                  }
                });
              }
            })
          })
        }
      })
      console.log(that.curPens);
    },
  },
}